const app = getApp()
import {
  getPlayletList
} from "../../api/modules/play";
Page({
  data: {
    playlets: [],
  },
  loading: false,
  pageable: {
    pageNum: 1,
    pageSize: 12,
    total: 0,
    pages: 0
  },
  onLoad: function () {
    this.fetchData(1)
  },

  async fetchData(pageNum) {
    if (this.loading) return;
    this.loading = true;

    try {
      const {
        data
      } = await getPlayletList({
        pageNum,
        pageSize: this.pageable.pageSize
      });
      let {
        list,
        ...pageable
      } = data;
      this.pageable = pageable;
      list = list || [];
      this.setData({
        playlets: pageNum === 1 ? list : this.data.playlets.concat(list)
      })

      if (this.data.playlets.length >= this.pageable.total && pageNum !== 1) {
        tt.showToast({
          title: '到底了～～'
        });
      }
    } catch {

    } finally {
      this.loading = false;
    }

  },

  onReachBottom() {
    if (this.pageable.total <= this.data.playlets.length) return;
    this.fetchData(this.pageable.pageNum + 1);
  },
  goVideoPlayer(e) {
    const goodsId = e.target.dataset.id;
    const title = e.target.dataset.title;
    tt.navigateTo({
      url: `/pages/videoPlayer/videoPlayer?goodsId=${goodsId}&title=${title}&eNum=1`
    });
  }
})